﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AGUnitEditor.Windows;
using AG.Data;

namespace AGUnitEditor
{
    public partial class MainWindow : Form
    {
        public MainWindow()
        {
            InitializeComponent();
            BindUnitTree();
        }

        private void _ctlBtnCreate_Click(object sender, EventArgs e)
        {
            AGCreateUnitWindow window = new AGCreateUnitWindow();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                AGUEContext.Current.Data.UnitList.Add(window.CreatedUnit);
                BindUnitTree();
            }
        }

        private void _ctlBtnSave_Click(object sender, EventArgs e)
        {
            string saveFolder = string.Format("{0}data\\", AppDomain.CurrentDomain.BaseDirectory);
            AGUEContext.Current.UnitStore.Save(saveFolder, AGUEContext.Current.Data.UnitCategoryList, AGUEContext.Current.Data.UnitList);
            MessageBox.Show("保存成功!");
        }

        private void BindUnitTree()
        {
            treeView1.Nodes.Clear();

            for (int cIndex = 0; cIndex < AGUEContext.Current.Data.UnitCategoryList.Count; cIndex++)
            {
                AGUnitCategory category = AGUEContext.Current.Data.UnitCategoryList[cIndex];
                TreeNode node = new TreeNode();
                node.Text = category.ToString();
                node.Tag = category;
                treeView1.Nodes.Add(node);

                for (int index = 0; index < AGUEContext.Current.Data.UnitList.Count; index++)
                {
                    AGUnit unit = AGUEContext.Current.Data.UnitList[index];
                    if (unit.CategoryId == category.Id)
                    {
                        TreeNode unitNode = new TreeNode();
                        unitNode.Text = unit.ToString();
                        unitNode.Tag = unit;
                        node.Nodes.Add(unitNode);
                    }
                }
            }
        }
    }
}
